<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username', 50)->unique()->comment('用户名');
            $table->string('phone', 20)->unique()->comment('手机号');
            $table->string('password')->comment('密码');
            $table->string('avatar')->nullable()->comment('头像路径');
            $table->enum('status', ['active', 'inactive', 'locked'])->default('active')->comment('账号状态');
            $table->json('preferences')->nullable()->comment('用户偏好设置');
            $table->timestamp('last_login_at')->nullable()->comment('最后登录时间');
            $table->string('last_login_ip', 45)->nullable()->comment('最后登录IP');
            $table->string('last_login_device')->nullable()->comment('最后登录设备');
            $table->timestamp('password_changed_at')->nullable()->comment('密码最后修改时间');
            $table->rememberToken();
            $table->timestamps();
            
            // 索引
            $table->index(['username', 'status']);
            $table->index(['phone', 'status']);
            $table->index('last_login_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};
